package org.apache.nutch.indexwriter.song;

import org.apache.hadoop.conf.Configuration;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.nutch.util.ObjectCache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.wltea.analyzer.lucene.IKAnalyzer4Lucene7;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * @Author Joycessh
 * @Description 单例：饿汉模式 -> DCl双检查锁机制
 * @Date 5/2/19 12:12 AM
 */
public class IndexWriterUtil {

    public static final Logger LOG = LoggerFactory
            .getLogger(IndexWriterUtil.class);

    private String directoryPath = "file:/opt/tmp/index";

    private int batchSize = 300;

    public volatile static IndexWriter instance;

    public static IndexWriter getInstance() throws IOException {
//        ObjectCache cache = ObjectCache.get(conf);
//        IndexWriter instance = (IndexWriter) cache
//                .getObject(IndexWriter.class.getName());
        if (instance != null) {

        }else {
            synchronized (IndexWriter.class) {
                if (instance == null) {
                    IndexWriterUtil indexWriterUtil = new IndexWriterUtil();
                    instance = indexWriterUtil.getIndexWriter();
//                    cache.setObject(IndexWriter.class.getName(), instance);
                }
            }
        }
//        return (IndexWriter) cache.getObject(IndexWriter.class.getName());
        return instance;
    }

    public IndexWriter getIndexWriter() throws IOException {
        //加载配置文件
        InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("song.properties");
        //conf.addResource(inputStream);
        Properties properties = new Properties();
        try{
            properties.load(inputStream);
        }catch (IOException ioE){
            ioE.printStackTrace();
        }finally{
            inputStream.close();
        }

        LOG.info("SONG INDEXER 配置文件加载完成");

        if (!properties.getProperty(SongConstants.DIRECTORY_PATH).isEmpty()) {
            directoryPath = properties.getProperty(SongConstants.DIRECTORY_PATH);
        }

        if (!properties.getProperty(SongConstants.COMMIT_SIZE).isEmpty()) {
            batchSize = Integer.parseInt(properties
                    .getProperty(SongConstants.COMMIT_SIZE));
        }
        LOG.info("directoryPath: " + directoryPath);
        LOG.info("batchSize: " + batchSize);

        //String directoryPath = conf.get(SongConstants.DIRECTORY_PATH);
        if (directoryPath == null) {
            String message = "Missing DIRECTORY PATH. Should be set in song.conf "
                    + SongConstants.DIRECTORY_PATH;
//            message += "\n" + describe();
            LOG.error(message);
            throw new RuntimeException(message);
        }
        //创建索引存放目录，文件系统
        Directory directory = FSDirectory.open((new File(directoryPath)).toPath());
        //创建分词器
        Analyzer analyzer = new IKAnalyzer4Lucene7(true);
        // 索引配置对象
        IndexWriterConfig config = new IndexWriterConfig(analyzer);
        // 设置索引库的打开模式：新建、追加、新建或追加
        //索引默认打开策略，存在则追加，不存在则创建
        config.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND);

        IndexWriter indexWriter = new org.apache.lucene.index.IndexWriter(directory, config);

        //索引优化,合并
        indexWriter.forceMerge(1);

        return indexWriter;
    }

    public String getDirectoryPath() {
        return directoryPath;
    }

    public int getBatchSize() {
        return batchSize;
    }
}
